package com.cloud.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.cloud.model.GameData;

/**
 * 修改database Created by Donghua.Chen on 2018/6/7.
 */
@Configuration
public class RedisCacheAutoConfiguration {

	@Autowired
	private RedisConnectionFactory factory;

	// @Bean
	// public StringRedisTemplate redisCacheTemplate(LettuceConnectionFactory
	// redisConnectionFactory) {
	// StringRedisTemplate template = new StringRedisTemplate();
	//// template.setKeySerializer(new StringRedisSerializer());
	//// template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
	// template.setConnectionFactory(redisConnectionFactory);
	// return template;
	// }

//	@Bean
//	public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(
//			ReactiveRedisConnectionFactory connectionFactory) {
//		return new ReactiveRedisTemplate<String, String>(connectionFactory, RedisSerializationContext.string());
//	}

	// @Bean
	// public ReactiveRedisTemplate<String, Object>
	// newReactiveRedisOperationsSessionRepository(ReactiveRedisConnectionFactory
	// connectionFactory) {
	// RedisSerializationContext<String, Object> serializationContext =
	// RedisSerializationContext
	// .<String, Object>newSerializationContext(
	// new GenericJackson2JsonRedisSerializer())
	// .build();
	//
	// ReactiveRedisTemplate<String, Object> redisTemplate = new
	// ReactiveRedisTemplate<>(
	// connectionFactory, serializationContext);
	// return redisTemplate;
	// }

	@Bean
	public ReactiveRedisTemplate<String, ?> createReactiveRedisTemplate(
			ReactiveRedisConnectionFactory connectionFactory) {
		RedisSerializer<String> keySerializer = new StringRedisSerializer();
		RedisSerializer<Object> valueSerializer = new GenericJackson2JsonRedisSerializer();
		RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
				.<String, Object>newSerializationContext(valueSerializer).key(keySerializer).hashKey(keySerializer)
				.build();
		return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
	}

	@Bean
	public ReactiveRedisConnection connection(ReactiveRedisConnectionFactory connectionFactory) {
		return connectionFactory.getReactiveConnection();
	}

	// public @PreDestroy void flushDb() {
	// factory.getConnection().flushDb();
	// }
}
